<?php
class Product_Model_Attached
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Product_Model_DbTable_Attached();
	}
	
	public static function getInstance(){
		return new Product_Model_Attached();
	}
	/**
	*Create A Record  
	*@param rowSet $row Product_Attached's data 
	*@return boolean
	*/
	public  function create($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row Product_Attached's data 
	*@param int $product_id 
	*@return boolean
	*/
	public  function update($row,$id){
	
		$where = $this->_table->getAdapter()->quoteInto("product_id= ?", $id);
 
		return $this->_table->update($row, $where);
		
	}
		
	/**
	*Delete One Row
	*@param int $product_id 
	*@return boolean
	*/
	public function delete($id){
	
		$where = $this->_table->getAdapter()->quoteInto("product_id= ?", $id);
 
		return $this->_table->delete($where);
		
	}	

    public function getByCondition($condition,$type='*',$orderby=array('pa_sort'),$pageSize=0,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["pa_file_type"]) && $condition["pa_file_type"]!=''){
            $select->where(" pa_file_type = ?",$condition["pa_file_type"]);
        }
		if(isset($condition["product_id"]) && $condition["product_id"]!=''){
            $select->where(" product_id = ?",$condition["product_id"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
		
	}	
	
	/**
	* 通过产品id查找图片
	* @param $productId
	* @return Zend_db_table_rowset
	*/
	public function getByProductId($productId){
		$where = $this->_table->select()->where("product_id = ?",$productId)->where("pa_file_type=?","img");
		$rowset = $this->_table->fetchAll($where,"pa_sort asc");
		return $rowset;
	}

	/**
	* 通过产品id查找links
	* @param $productId
	* @return Zend_db_table_rowset
	*/
	public function getLinksByProductId($productId){
		$where = $this->_table->select()->where("product_id = ?",$productId)->where("pa_file_type=?","link");
		$rowset = $this->_table->fetchAll($where,"pa_sort asc");
		return $rowset;
	}
	/**
	* 通过产品id删除图片
	* @param $productId
	* @return int
	*/
	public function deleteImgByProductId($productId){
		$where = $this->_table->getAdapter()->quoteInto("product_id = ?",$productId);
		$where .= $this->_table->getAdapter()->quoteInto(" and pa_file_type=?","img");
		return $this->_table->delete($where);
	}
	/**
	* 通过产品id删除links
	* @param $productId
	* @return int
	*/
	public function deleteLinksByProductId($productId){
		$where = $this->_table->getAdapter()->quoteInto("product_id = ?",$productId);
		$where .= $this->_table->getAdapter()->quoteInto(" and pa_file_type=?","link");
		return $this->_table->delete($where);
	}
	
	public function getByfield($field=1,$value=1){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchAll($where);
	}
	
	/**
	* 获得产品展示图片
	*
	*/
	public function getViewPicture($productId){
		$where = $this->_table->select()->where("product_id = ?",$productId)->where("(pa_file_type=?","img")->orWhere("pa_file_type=?)","link");
		$rowset = $this->_table->fetchRow($where,"pa_sort asc");
		return $rowset;
	}
	/**
	* 获得产品所有展示图片
	*
	*/
	public function getPicturesPath($productId){
		$where = $this->_table->select()->where("product_id = ?",$productId)->where("(pa_file_type=?","img")->orWhere("pa_file_type=?)","link");
		$res = $this->_table->fetchAll($where,"pa_sort asc");
		return $res;
	}
	
	public function getByPaid($paid)	{
		$where = $this->_table->select()->where("id = ?",$paid);
		$row = $this->_table->fetchRow($where);
		return $row;
	}
}